﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace EPiServer.CdnSupport.Test
{
    using EPiServer.Web;
    using EPiServer.Web.Hosting;
    using System.Collections.Specialized;
    using EPiServer.Configuration;
    using System.Configuration;
    using EPiServer.Core;

    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class TestValidation
    {
        public TestValidation()
        {
        }

        public Cdn cdn;

        private UrlRewriteEventArgs GetValidUrlRewriteEventArgs()
        {
            return GetValidUrlRewriteEventArgs("/PageFiles/logo.png");
        }

        private UrlRewriteEventArgs GetValidUrlRewriteEventArgs(string url)
        {
            UrlRewriteEventArgs e = new UrlRewriteEventArgs(new UrlBuilder(url));
            e.IsModified = false;
            return e;
        }

        [TestInitialize]
        public void Init()
        {
            cdn = new Cdn("http://localhost", false, ".png");

            GenericHostingEnvironment.Instance = new UnitTestHostingEnvironment();
            NameValueCollection coll = new NameValueCollection();
            coll.Add("virtualPath", "~/PageFiles/");
            coll.Add("name", "SitePageFiles");
            TestVirtualPathProvider vpp = new TestVirtualPathProvider("testVpp", coll);
            GenericHostingEnvironment.RegisterVirtualPathProvider(vpp);
        }

        [TestMethod]
        public void Verify_Valid_Urls()
        {
            Assert.IsTrue(cdn.CanRewriteToCdn(GetValidUrlRewriteEventArgs()));
        }

        [TestMethod]
        public void Should_Not_Rewrite_Pages()
        {
            UrlRewriteEventArgs e = GetValidUrlRewriteEventArgs();
            e.Internal = new PageReference(3);
            Assert.IsFalse(cdn.CanRewriteToCdn(e));
        }


        [TestMethod]
        public void Should_Not_Rewrite_Url_with_Authority()
        {
            UrlRewriteEventArgs e = GetValidUrlRewriteEventArgs("http://localhost/PageFiles/Logo.png");
            Assert.IsFalse(cdn.CanRewriteToCdn(e));
        }

        [TestMethod]
        public void Should_Not_Rewrite_Relative_Urls()
        {
            UrlRewriteEventArgs e = GetValidUrlRewriteEventArgs("PageFiles/Logo.png");
            Assert.IsFalse(cdn.CanRewriteToCdn(e));
        }

        [TestMethod]
        public void Should_Not_Rewrite_Url_with_Querystring()
        {
            UrlRewriteEventArgs e = GetValidUrlRewriteEventArgs("/PageFiles/Logo.png?query=string");
            Assert.IsFalse(cdn.CanRewriteToCdn(e));
        }

        [TestMethod]
        public void Should_Not_Rewrite_Url_without_Extension()
        {
            UrlRewriteEventArgs e = GetValidUrlRewriteEventArgs("/PageFiles/Logo");
            Assert.IsFalse(cdn.CanRewriteToCdn(e));
        }

        [TestMethod]
        public void Should_rewrite_Specified_Extensions_Only()
        {
            UrlRewriteEventArgs e = GetValidUrlRewriteEventArgs("/PageFiles/Logo.png");
            Assert.IsTrue(cdn.CanRewriteToCdn(e));

            e = GetValidUrlRewriteEventArgs("/PageFiles/Logo.dat");
            Assert.IsFalse(cdn.CanRewriteToCdn(e));

            Cdn cdn2 = new Cdn("http://localhost", false, "*");
            Assert.IsTrue(cdn2.CanRewriteToCdn(e));
        }

        [TestMethod]
        public void Should_Handle_Page_Files()
        {
            UrlRewriteEventArgs e = GetValidUrlRewriteEventArgs("/PageFiles/Logo.png");
            Assert.IsTrue(cdn.CanRewriteToCdn(e));
            Cdn cdn2 = new Cdn("http://localhost", false, ".png",false,"/PageFiles");
            Assert.IsFalse(cdn2.CanRewriteToCdn(e));
        }
    }
}
